package com.rabbitmq.learn.Simple;

import com.rabbitmq.client.*;
import com.rabbitmq.learn.common.ConnectionUtils;
import com.rabbitmq.learn.common.RabbitMqConstant;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Created by Lxk on 2019/8/11.
 */
@SuppressWarnings("Duplicates")
public class Receiver2 {

    /**
     * Simple模式下，启动两个监听者，第二个启动的收不到消息
     * @param args
     * @throws IOException
     * @throws TimeoutException
     */
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection con = ConnectionUtils.getConnection();

        Channel channel = con.createChannel();

        channel.queueDeclare(RabbitMqConstant.Simple.QUEUE_NAME, false, false, false, null);

        DefaultConsumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String msg = new String(body, "utf-8");
                System.out.println("msg:" + msg);
            }
        };

        channel.basicConsume(RabbitMqConstant.Simple.QUEUE_NAME, true, consumer);

    }

}
